package com.cqc.gridview03;

import android.content.Context;
import android.content.Intent;
import android.graphics.Point;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

import me.nereo.multi_image_selector.MultiImageSelector;
import me.nereo.multi_image_selector.MultiImageSelectorActivity;

public class MainActivity1 extends AppCompatActivity {

    private GridView gridView;
    private List<String> pathList = new ArrayList<>();
    private MyAdapter adapter;
    private int limitCount = 5;//最多选择3张

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        gridView = (GridView) findViewById(R.id.gridView);
        adapter = new MyAdapter();
        gridView.setAdapter(adapter);

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(MainActivity1.this, position + "", Toast.LENGTH_SHORT).show();
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 100) {
            if (resultCode == RESULT_OK) {
                pathList.clear();
                pathList.addAll(data.getStringArrayListExtra(MultiImageSelectorActivity.EXTRA_RESULT));
                adapter.notifyDataSetChanged();
            }
        }
    }

    public class MyAdapter extends BaseAdapter {
        int mItemWidth;

        public MyAdapter() {
            WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
            int width = 0;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
                Point size = new Point();
                wm.getDefaultDisplay().getSize(size);
                width = size.x;
            } else {//getWidth()在API>=13后 过时了，
                width = wm.getDefaultDisplay().getWidth();
            }
            mItemWidth = width / 3;
        }

        @Override
        public int getCount() {
            return pathList.size() + 1;
        }

        @Override
        public Object getItem(int position) {
            if (pathList.size() == 0) {
                return null;
            } else {
                return pathList.get(position-1);
            }
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item1, parent, false);
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

//            ViewGroup.LayoutParams params = holder.iv.getLayoutParams();
//            params.width = mItemWidth;
//            params.height = mItemWidth;
//            holder.iv.setLayoutParams(params);

            ViewGroup.LayoutParams params = convertView.getLayoutParams();
            params.width = mItemWidth;
            params.height = mItemWidth;
            convertView.setLayoutParams(params);

//            Log.d("LogUtil", "position=" + position + ",pathList.size()" + pathList.size());

            if (position == pathList.size()) {
                //添加图片
                holder.iv.setVisibility(View.GONE);//内容图片隐藏
                holder.iv_add.setVisibility(View.VISIBLE);//显示加号图片
                holder.iv_delete.setVisibility(View.GONE);
            } else {
                //显示图片
                holder.iv_add.setVisibility(View.GONE);//加号图片隐藏
                holder.iv.setVisibility(View.VISIBLE);//内容图片显示
                holder.iv_delete.setVisibility(View.VISIBLE);
                Glide.with(parent.getContext()).load("file://" + pathList.get(position)).into(holder.iv);
                holder.iv_delete.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        pathList.remove(position);
                        notifyDataSetChanged();
                    }
                });
            }
            holder.iv_add.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    MultiImageSelector.create().count(limitCount).start(MainActivity1.this, 100);
                }
            });

            return convertView;
        }
    }

    public class ViewHolder {

        ImageView iv;
        ImageView iv_add;
        ImageView iv_delete;

        public ViewHolder(View itemView) {
            super();
            iv = (ImageView) itemView.findViewById(R.id.iv);
            iv_add = (ImageView) itemView.findViewById(R.id.iv_add);
            iv_delete = (ImageView) itemView.findViewById(R.id.iv_delete);
        }
    }
}
